<?php
/**
 * 报警设置
 * Created by PhpStorm.
 * User: Earl <guo@seosite.cn>
 * Date: 2017/9/27
 * Time: 上午10:14
 * Updatetime:
 * updater：
 */
namespace WAdmin\Controller;

class AlarmController extends BaseController{

    /**
     * @desc
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function index($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);


        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $model = M('car');


        if (!empty($param['keyword'])){
            $map['car_no'] = array('like','%'.$param['keyword'].'%');
            $map['uid']= $this->userInfo->id;

            $ids = $model->where($map)->getField('id',true);

            $idsStr = implode(',',$ids);

			$map_equipment['car_id'] = array('in',$idsStr);
        }

        $model_car_equipment = M('car_equipment');

        $map_equipment['uid'] = $this->userInfo->id;

        list($list,$page) = _listPage($model_car_equipment,$map_equipment,$p,'id desc',null,$listRows);

        if (!empty($list)){
            foreach ($list as $k=>$v){
            	$car_no = $model->where(['id'=>$v['car_id']])->getField('car_no');

				$list[$k]['car_no'] = $car_no;



				if (!empty($v['speed'])){
					$list[$k]['event'] = '加速报警,';
					$list[$k]['content'] = '加速报警限制'.$v['speed'].',';
				}

				if (!empty($v['outside_area'])){
					$list[$k]['event'] = '出区域报警,';
					$list[$k]['content'] = '出区域报警限制'.$v['outside_area'].',';
				}

				if (!empty($v['inside_area'])){
					$list[$k]['event'] = '入区域报警,';
					$list[$k]['content'] = '入区域报警限制'.$v['inside_area'].',';
				}

				if ($v['battery_dismantle']==1){
					$list[$k]['event'] = '电频拆除报警,';
					$list[$k]['content'] = '电频拆除报警开,';
				}

				if (!empty($v['temperature'])){
					$list[$k]['event'] = '温度报警,';
					$list[$k]['content'] = '温度限制'.$v['temperature'].',';
				}

				if ($v['unlawful_opening']==1){
					$list[$k]['event'] = '非法开门报警,';
					$list[$k]['content'] = '非法开门报警开,';
				}

				if ($v['parking_overtime']==1){
					$list[$k]['event'] = '停车超时报警,';
					$list[$k]['content'] = '停车超时报警开';
				}

				if ($v['offline']==1){
					$list[$k]['event'] = '掉线报警,';
					$list[$k]['content'] = '掉线报警开';
				}

				if ($v['online']==1){
					$list[$k]['event'] = '上线报警,';
					$list[$k]['content'] = '上线报警开';
				}

				if ($v['reversal']==1){
					$list[$k]['event'] = '反转报警,';
					$list[$k]['content'] = '反转报警开';
				}

				if ($v['shock']==1){
					$list[$k]['event'] = '震动报警,';
					$list[$k]['content'] = '震动报警开';
				}


				if ($v['ignition']==1){
					$list[$k]['event'] = '点火报警,';
					$list[$k]['content'] = '点火报警开';
				}

				if ($v['stop_ignition']==1){
					$list[$k]['event'] = '停车点火报警,';
					$list[$k]['content'] = '停车点火报警开';
				}


				if ($v['gate_magnetic']==1){
					$list[$k]['event'] = '门开关报警,';
					$list[$k]['content'] = '门开关报警开';
				}

				if ($v['ignition_line']==1){
					$list[$k]['event'] = '点火线报警,';
					$list[$k]['content'] = '点火线报警开';
				}

				if (!empty($v['low_voltage'])){
					$list[$k]['event'] = '低电压报警,';
					$list[$k]['content'] = '低电压限制'.$v['low_voltage'].',';
				}

//				if (!empty($v['temperature_sensor'])){
//					$list[$k]['event'] = '出报警,';
//					$list[$k]['content'] = '出报警限制'.$v['temperature_sensor'].',';
//				}
//
//				if (!empty($v['outside_area'])){
//					$list[$k]['event'] = '出报警,';
//					$list[$k]['content'] = '出报警限制'.$v['outside_area'].',';
//				}


				if (!empty($v['oil_sensor'])){
					$list[$k]['event'] = '油量报警,';
					$list[$k]['content'] = '油量限制'.$v['oil_sensor'].',';
				}

				if (!empty($v['humidity'])){
					$list[$k]['event'] = '湿度报警,';
					$list[$k]['content'] = '湿度限制'.$v['humidity'].',';
				}

				if (!empty($v['one_zone_temperature'])){
					$list[$k]['event'] = '一区温度报警,';
					$list[$k]['content'] = '一区温度限制'.$v['one_zone_temperature'].',';
				}

				if (!empty($v['one_zone_humidity'])){
					$list[$k]['event'] = '一区湿度报警,';
					$list[$k]['content'] = '一区湿度限制'.$v['one_zone_humidity'].',';
				}


				if (!empty($v['two_zone_temperature'])){
					$list[$k]['event'] = '二区温度报警,';
					$list[$k]['content'] = '二区温度限制'.$v['one_zone_temperature'].',';
				}

				if (!empty($v['two_zone_humidity'])){
					$list[$k]['event'] = '二区湿度报警,';
					$list[$k]['content'] = '二区湿度限制'.$v['one_zone_humidity'].',';
				}

				if (!empty($v['three_zone_temperature'])){
					$list[$k]['event'] = '三区温度报警,';
					$list[$k]['content'] = '三区温度限制'.$v['one_zone_temperature'].',';
				}

				if (!empty($v['three_zone_humidity'])){
					$list[$k]['event'] = '三区湿度报警,';
					$list[$k]['content'] = '三区湿度限制'.$v['one_zone_humidity'].',';
				}

				if (!empty($v['four_zone_temperature'])){
					$list[$k]['event'] = '四区温度报警,';
					$list[$k]['content'] = '四区温度限制'.$v['one_zone_temperature'].',';
				}

				if (!empty($v['four_zone_humidity'])){
					$list[$k]['event'] = '四区湿度报警,';
					$list[$k]['content'] = '四区湿度限制'.$v['one_zone_humidity'].',';
				}

				if (!empty($v['refrigerating_machine'])){
					$list[$k]['event'] = '制冷机开报警,';
					$list[$k]['content'] = '制冷机开报警限制'.$v['refrigerating_machine'].',';
				}

				if (!empty($v['refrigeration_alarm'])){
					$list[$k]['event'] = '制冷机关,';
					$list[$k]['content'] = '制冷机关限制'.$v['refrigeration_alarm'].',';
				}

				if (!empty($v['high_voltage'])){
					$list[$k]['event'] = '高电压报警,';
					$list[$k]['content'] = '高电压报警限制'.$v['high_voltage'].',';
				}


				if (empty($car_no) || empty($list[$k]['event']) || empty($list[$k]['content'])){
					$list[$k]['event'] = '';
					$list[$k]['content'] = '';
					$list[$k]['is_set_alarm'] = '2';
				}else{
					$list[$k]['is_set_alarm'] = '1';
				}

                $list[$k]['start_time'] = date('Y-m-d\TH:i:s.0000\Z', $v['start_time']);
                $list[$k]['end_time'] = date('Y-m-d\TH:i:s.0000\Z', $v['end_time']);
                $list[$k]['re_time'] = date('Y-m-d\TH:i:s.0000\Z', $v['re_time']);
            }
        }

        return showList($list,$page);
    }


    /**
     * @desc 报警详情
     * @param $param
     * @return array
     * @author Egor<http://gitee.com/seosite>
     */
    public function detail($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $equipment = M('car_equipment')->find($param['id']);

        return showErrorMsg(L('_OPERATION_SUCCESS_'),0,2,$equipment);
    }

    public function save($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $model_car_equipment = M('car_equipment');


		$save_data_equipment = [
			'id'    =>  $param['id'],
			'speed'     =>  empty($param['speed'])?'':$param['speed'],
			'outside_area'     =>  empty($param['outside_area'])?'':$param['outside_area'],
			'inside_area'     =>  empty($param['inside_area'])?'':$param['inside_area'],
			'battery_dismantle'     =>  empty($param['battery_dismantle'])?'':$param['battery_dismantle'],
			'temperature'     =>  empty($param['temperature'])?'':$param['temperature'],
			'unlawful_opening'     =>  empty($param['unlawful_opening'])?'':$param['unlawful_opening'],
			'parking_overtime'     =>  empty($param['parking_overtime'])?'':$param['parking_overtime'],
			'offline'     =>  empty($param['offline'])?'':$param['offline'],
			'online'     =>  empty($param['online'])?'':$param['online'],
			'reversal'     =>  empty($param['reversal'])?'':$param['reversal'],
			'shock'     =>  empty($param['shock'])?'':$param['shock'],
			'door'     =>  empty($param['door'])?'':$param['door'],
			'start'     =>  empty($param['start'])?'':$param['start'],
			'cold_open'     =>  empty($param['cold_open'])?'':$param['cold_open'],
			'cold_close'     =>  empty($param['cold_close'])?'':$param['cold_close'],
			'low_voltage'     =>  empty($param['low_voltage'])?'':$param['low_voltage'],
			'ignition'     =>  empty($param['ignition'])?'':$param['ignition'],
			'stop_ignition'     =>  empty($param['stop_ignition'])?'':$param['stop_ignition'],
			'temperature_sensor'     =>  empty($param['temperature_sensor'])?'':$param['temperature_sensor'],
			'gate_magnetic'     =>  empty($param['gate_magnetic'])?'':$param['gate_magnetic'],
			'ignition_line'     =>  empty($param['ignition_line'])?'':$param['ignition_line'],
			'oil_sensor'     =>  empty($param['oil_sensor'])?'':$param['oil_sensor'],
			'humidity'     =>  empty($param['humidity'])?'':$param['humidity'],
			'one_zone_temperature'     =>  empty($param['one_zone_temperature'])?'':$param['one_zone_temperature'],
			'one_zone_humidity'     =>  empty($param['one_zone_humidity'])?'':$param['one_zone_humidity'],
			'two_zone_temperature'     =>  empty($param['two_zone_temperature'])?'':$param['two_zone_temperature'],
			'two_zone_humidity'     =>  empty($param['two_zone_humidity'])?'':$param['two_zone_humidity'],
			'three_zone_temperature'     =>  empty($param['three_zone_temperature'])?'':$param['three_zone_temperature'],
			'three_zone_humidity'     =>  empty($param['three_zone_humidity'])?'':$param['three_zone_humidity'],
			'four_zone_temperature'     =>  empty($param['four_zone_temperature'])?'':$param['four_zone_temperature'],
			'four_zone_humidity'     =>  empty($param['four_zone_humidity'])?'':$param['four_zone_humidity'],
			'refrigerating_machine'     =>  empty($param['refrigerating_machine'])?'':$param['refrigerating_machine'],
			'refrigeration_alarm'     =>  empty($param['refrigeration_alarm'])?'':$param['refrigeration_alarm'],
			'high_voltage'     =>  empty($param['high_voltage'])?'':$param['high_voltage'],
			'start_time'     =>  empty($param['start_time'])?'':$param['start_time'],
			'end_time'     =>  empty($param['end_time'])?'':$param['end_time'],
			're_time'     =>  empty($param['re_time'])?'':$param['re_time'],
			'refresh_time'     =>  empty($param['refresh_time'])?'':$param['refresh_time'],
		];

        $model_car_equipment->save($save_data_equipment);


        return showErrorMsg(L("_OPERATION_SUCCESS_"));
    }






    public function temAlarm($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);


        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $model = M('car');


        if (!empty($param['keyword'])){
            $map['car_no'] = array('like','%'.$param['keyword'].'%');
        }

        $model_car_equipment = M('car_equipment');

        $map['uid'] = $this->userInfo->id;

        list($list,$page) = _listPage($model,$map,$p,'id desc','id,car_no',$listRows);

        if (!empty($list)){
            foreach ($list as $k=>$v){

                $carEquipmentInfo = $model_car_equipment->where(['car_id'=>$v['id']])->find();

                if (empty($carEquipmentInfo)){
                    $list[$k]['event'] = '';
                    $list[$k]['content'] = '';
                    $list[$k]['is_sealarm'] = '2';
                }

                $list[$k]['is_sealarm'] = '1';

                if (!empty($carEquipmentInfo['speed'])){
                    $list[$k]['event'] = '加速报警,';
                    $list[$k]['content'] = '加速报警限制'.$carEquipmentInfo['speed'].',';
                }

                if (!empty($carEquipmentInfo['outside_area'])){
                    $list[$k]['event'] = '出区域报警,';
                    $list[$k]['content'] = '出区域报警限制'.$carEquipmentInfo['outside_area'].',';
                }

                if (!empty($carEquipmentInfo['inside_area'])){
                    $list[$k]['event'] = '入区域报警,';
                    $list[$k]['content'] = '入区域报警限制'.$carEquipmentInfo['inside_area'].',';
                }

                if ($carEquipmentInfo['battery_dismantle']==1){
                    $list[$k]['event'] = '电频拆除报警,';
                    $list[$k]['content'] = '电频拆除报警开,';
                }

                if (!empty($carEquipmentInfo['temperature'])){
                    $list[$k]['event'] = '温度报警,';
                    $list[$k]['content'] = '温度限制'.$carEquipmentInfo['temperature'].',';
                }

                if ($carEquipmentInfo['unlawful_opening']==1){
                    $list[$k]['event'] = '非法开门报警,';
                    $list[$k]['content'] = '非法开门报警开,';
                }

                if ($carEquipmentInfo['parking_overtime']==1){
                    $list[$k]['event'] = '停车超时报警,';
                    $list[$k]['content'] = '停车超时报警开';
                }

                if ($carEquipmentInfo['offline']==1){
                    $list[$k]['event'] = '掉线报警,';
                    $list[$k]['content'] = '掉线报警开';
                }

                if ($carEquipmentInfo['online']==1){
                    $list[$k]['event'] = '上线报警,';
                    $list[$k]['content'] = '上线报警开';
                }

                if ($carEquipmentInfo['reversal']==1){
                    $list[$k]['event'] = '反转报警,';
                    $list[$k]['content'] = '反转报警开';
                }

                if ($carEquipmentInfo['shock']==1){
                    $list[$k]['event'] = '震动报警,';
                    $list[$k]['content'] = '震动报警开';
                }


                if ($carEquipmentInfo['ignition']==1){
                    $list[$k]['event'] = '点火报警,';
                    $list[$k]['content'] = '点火报警开';
                }

                if ($carEquipmentInfo['stop_ignition']==1){
                    $list[$k]['event'] = '停车点火报警,';
                    $list[$k]['content'] = '停车点火报警开';
                }


                if ($carEquipmentInfo['gate_magnetic']==1){
                    $list[$k]['event'] = '门开关报警,';
                    $list[$k]['content'] = '门开关报警开';
                }

                if ($carEquipmentInfo['ignition_line']==1){
                    $list[$k]['event'] = '点火线报警,';
                    $list[$k]['content'] = '点火线报警开';
                }

                if (!empty($carEquipmentInfo['low_voltage'])){
                    $list[$k]['event'] = '低电压报警,';
                    $list[$k]['content'] = '低电压限制'.$carEquipmentInfo['low_voltage'].',';
                }

                if (!empty($carEquipmentInfo['temperature_sensor'])){
                    $list[$k]['event'] = '出报警,';
                    $list[$k]['content'] = '出报警限制'.$carEquipmentInfo['temperature_sensor'].',';
                }

                if (!empty($carEquipmentInfo['outside_area'])){
                    $list[$k]['event'] = '出报警,';
                    $list[$k]['content'] = '出报警限制'.$carEquipmentInfo['outside_area'].',';
                }


                if (!empty($carEquipmentInfo['oil_sensor'])){
                    $list[$k]['event'] = '油量报警,';
                    $list[$k]['content'] = '油量限制'.$carEquipmentInfo['oil_sensor'].',';
                }

                if (!empty($carEquipmentInfo['humidity'])){
                    $list[$k]['event'] = '湿度报警,';
                    $list[$k]['content'] = '湿度限制'.$carEquipmentInfo['humidity'].',';
                }

                if (!empty($carEquipmentInfo['one_zone_temperature'])){
                    $list[$k]['event'] = '一区温度报警,';
                    $list[$k]['content'] = '一区温度限制'.$carEquipmentInfo['one_zone_temperature'].',';
                }

                if (!empty($carEquipmentInfo['one_zone_humidity'])){
                    $list[$k]['event'] = '一区湿度报警,';
                    $list[$k]['content'] = '一区湿度限制'.$carEquipmentInfo['one_zone_humidity'].',';
                }


                if (!empty($carEquipmentInfo['two_zone_temperature'])){
                    $list[$k]['event'] = '二区温度报警,';
                    $list[$k]['content'] = '二区温度限制'.$carEquipmentInfo['one_zone_temperature'].',';
                }

                if (!empty($carEquipmentInfo['two_zone_humidity'])){
                    $list[$k]['event'] = '二区湿度报警,';
                    $list[$k]['content'] = '二区湿度限制'.$carEquipmentInfo['one_zone_humidity'].',';
                }

                if (!empty($carEquipmentInfo['three_zone_temperature'])){
                    $list[$k]['event'] = '三区温度报警,';
                    $list[$k]['content'] = '三区温度限制'.$carEquipmentInfo['one_zone_temperature'].',';
                }

                if (!empty($carEquipmentInfo['three_zone_humidity'])){
                    $list[$k]['event'] = '三区湿度报警,';
                    $list[$k]['content'] = '三区湿度限制'.$carEquipmentInfo['one_zone_humidity'].',';
                }

                if (!empty($carEquipmentInfo['four_zone_temperature'])){
                    $list[$k]['event'] = '四区温度报警,';
                    $list[$k]['content'] = '四区温度限制'.$carEquipmentInfo['one_zone_temperature'].',';
                }

                if (!empty($carEquipmentInfo['four_zone_humidity'])){
                    $list[$k]['event'] = '四区湿度报警,';
                    $list[$k]['content'] = '四区湿度限制'.$carEquipmentInfo['one_zone_humidity'].',';
                }

                if (!empty($carEquipmentInfo['refrigerating_machine'])){
                    $list[$k]['event'] = '制冷机开报警,';
                    $list[$k]['content'] = '制冷机开报警限制'.$carEquipmentInfo['refrigerating_machine'].',';
                }

                if (!empty($carEquipmentInfo['refrigeration_alarm'])){
                    $list[$k]['event'] = '制冷机关,';
                    $list[$k]['content'] = '制冷机关限制'.$carEquipmentInfo['refrigeration_alarm'].',';
                }

                if (!empty($carEquipmentInfo['high_voltage'])){
                    $list[$k]['event'] = '高电压报警,';
                    $list[$k]['content'] = '高电压报警限制'.$carEquipmentInfo['high_voltage'].',';
                }

                $list[$k]['create_time'] = $carEquipmentInfo['create_time'];

                $list[$k]['start_time'] = empty($carEquipmentInfo)?'':date('Y-m-d\TH:i:s.0000\Z', $carEquipmentInfo['start_time']);
                $list[$k]['end_time'] = empty($carEquipmentInfo)?'':date('Y-m-d\TH:i:s.0000\Z', $carEquipmentInfo['end_time']);
                $list[$k]['re_time'] = empty($carEquipmentInfo)?'':date('Y-m-d\TH:i:s.0000\Z', $carEquipmentInfo['re_time']);
            }
        }

        return showList($list,$page);
    }
}